package org.r.tools.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
import org.r.tools.code.ResCode;

import java.io.Serializable;
import java.util.List;

/**
 * 功能描述
 *
 * @author: R
 * @date: 2022年08月31日 9:14
 */
@Data
@Schema(description = "响应结果")
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResBody<T> implements Serializable {

    @Schema(description = "状态码")
    private String code;

    @Schema(description = "提示信息")
    private String message;

    @Schema(description = "响应数据")
    private T data;

    @Schema(description = "响应数据集合")
    private List<T> records; //数据集合

    private ResBody() {

    }

    private ResBody(T data) {
        this.data = data;
    }


    public static <T> ResBody<T> ok() {
        ResBody<T> resBody = new ResBody<>();
        resBody.setCode(ResCode.SUCCESS);
        resBody.setMessage("SUCCESS");
        return resBody;
    }

    public static <T> ResBody<T>  ok(T body) {
        ResBody<T> resBody = new ResBody<>(body);
        resBody.setCode(ResCode.SUCCESS);
        resBody.setMessage("SUCCESS");
        return resBody;
    }

    public static <T>ResBody<T> failed(){
        ResBody<T> resBody = new ResBody<>();
        resBody.setCode(ResCode.DEFAULT_ERROR);
        resBody.setMessage("FAILURE");
        return resBody;
    }


    public static <T> ResBody<T> failed(String message){
        ResBody<T> resBody = new ResBody<>();
        resBody.setCode(ResCode.DEFAULT_ERROR);
        resBody.setMessage(message);
        return resBody;
    }

    public static <T> ResBody<T> failed(String code, String message) {
        ResBody<T> resBody = new ResBody<>();
        resBody.setCode(code);
        resBody.setMessage(message);
        return resBody;
    }
}

